﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utils;
using System.Data;
using Utils.DatabaseUtils;
using Utils.DC.Web.UI;

namespace DCSLibrary
{
    public class FAQUI
    {
        private const String CACHE_KEY = "FAQ_GetAll";

        public int Add(int CategoryPageID, String FAQAlias, String PageTitle,
            String MetaKeyword, String MetaDescription, String Question, String Answer, byte Status, int ViewOrder, bool IsApproved, int ViewTime,
            DateTime CreateDate, String CreateBy)
        {
            try
            {
                int i = SqlHelper.ExecuteNonQuery(Common.Conn, "FAQ_add", CategoryPageID,  FAQAlias,  PageTitle,
             MetaKeyword,  MetaDescription,  Question,  Answer,  Status,  ViewOrder,  IsApproved,  ViewTime,
             CreateDate,  CreateBy);

                if (i > 0)
                {
                    WebUtility.RemoveCache(CACHE_KEY);
                }

                return i;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int Update(int FAQID, int CategoryPageID, String FAQAlias, String PageTitle,
            String MetaKeyword, String MetaDescription, String Question, String Answer, byte Status, int ViewOrder, bool IsApproved, int ViewTime,
            DateTime CreateDate, String CreateBy)
        {
            try
            {
                int i = SqlHelper.ExecuteNonQuery(Common.Conn, "FAQ_update", FAQID, CategoryPageID, FAQAlias, PageTitle,
             MetaKeyword, MetaDescription, Question, Answer, Status, ViewOrder, IsApproved, ViewTime,
             CreateDate, CreateBy);

                if (i > 0)
                {
                    WebUtility.RemoveCache(CACHE_KEY + "_" + FAQID);
                    WebUtility.RemoveCache(CACHE_KEY);
                }

                return i;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public DataSet GetAll()
        {
            try
            {
                object obj = WebUtility.GetCacheObject(CACHE_KEY);
                if (obj == null)
                {
                    DataSet ds = SqlHelper.ExecuteDataset(Common.Conn, CommandType.StoredProcedure, "FAQ_getall");
                    if (ds != null)
                    {
                        WebUtility.InsertCache(CACHE_KEY, ds);
                    }
                    return ds;
                }
                else
                {
                    return (DataSet)obj;
                }
            }
            catch (Exception e)
            {
                return null;
            }
        }

        public DataSet GetByID(int FAQID)
        {
            try
            {
                object obj = WebUtility.GetCacheObject(CACHE_KEY + "_" + FAQID.ToString());
                if (obj == null)
                {
                    DataSet ds = SqlHelper.ExecuteDataset(Common.Conn, "FAQ_get", FAQID);
                    if (ds != null)
                    {
                        WebUtility.InsertCache(CACHE_KEY + "_" + FAQID.ToString(), ds);
                    }
                    return ds;
                }
                else
                {
                    return (DataSet)obj;
                }
            }
            catch (Exception e)
            {
                return null;
            }
        }

        public int DeleteByID(int FAQID)
        {
            try
            {
                int i = SqlHelper.ExecuteNonQuery(Common.Conn, "FAQ_delete", FAQID);
                if (i > 0)
                {
                    WebUtility.RemoveCache(CACHE_KEY);
                }
                return i;
            }
            catch (Exception e)
            {
                return -1;
            }
        }
    }
}
